// -------------------------按钮打开-----------------------------------
// 获取全部元素并添加监听事件
document.addEventListener('DOMContentLoaded', function () {
    var dialog = document.getElementById('dialog');
    var openBtn = document.getElementById('openBtn');
    var closeBtn = document.getElementsByClassName('closeBtn')[0];

    openBtn.onclick = function () {
        dialog.style.display = "block";
    }

    closeBtn.onclick = function () {
        dialog.style.display = "none";
    }

    window.onclick = function (event) {
        if (event.target == dialog) {
            dialog.style.display = "none";
        }
    }
});

// -------------------------------------------------------------------


// -------------------------------验证码-------------------------------

function generateCode() {
    let code = ''
    let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    let charactersLength = characters.length
    for (let i = 0; i <= 6; i++) {
        code += characters.charAt(Math.floor(Math.random() * charactersLength))
    }
    return code
}

// 制作验证码
function drawCode() {
    var canvas = document.getElementById('verif');
    var ctx = canvas.getContext('2d');

    var code = generateCode();
    sessionStorage.setItem('captchaCode', code);

    ctx.clearRect(0, 0, canvas.width, canvas.height)
    ctx.font = '20px'
    ctx.fillText(code, 5, 25)
}

drawCode()



// 提交按钮
function codeOpen() {
    var userInput = document.getElementById('code').value.toLowerCase();
    var storedCode = sessionStorage.getItem('captchaCode').toLowerCase();
    if (userInput !== storedCode) {
        alert('验证码错误，请重新输入');
        drawCode();
    }
    setTimeout(function () {
        window.location.href = 'Login.html'
    }, 2000)
}

//判断内容是否为空
code.addEventListener('keyup', function (e) {
    if (e.key === 'Enter') {
        if (code.value === '') {
            alert('请输入验证码')
        } else {

            code.value = ''
            alert ('注册成功')
        }
    }
})      

//跳转

// -------------------------------------------------------------------

